//#region deepseek

class ColorAdjuster {
    constructor(hex) {
        this.hex = hex; // 不带#的6位HEX值
    }

    /* 核心工具方法 */
    #getNormalDelta(maxDelta, coefficient) {
        // 生成正态分布随机值 (Box-Muller变换)
        let u = 0, v = 0;
        while (u === 0) u = Math.random();
        while (v === 0) v = Math.random();
        const z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);

        // 应用系数并限制范围
        const delta = z * coefficient;
        return Math.round(Math.max(-maxDelta, Math.min(maxDelta, delta)));
    }

    #hexToHSL(hex) {
        // HEX转RGB
        const r = parseInt(hex.substr(0, 2), 16) / 255;
        const g = parseInt(hex.substr(2, 2), 16) / 255;
        const b = parseInt(hex.substr(4, 2), 16) / 255;

        // RGB转HSL
        const max = Math.max(r, g, b), min = Math.min(r, g, b);
        let h, s, l = (max + min) / 2;

        if (max === min) {
            h = s = 0; // achromatic
        } else {
            const d = max - min;
            s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
            switch (max) {
                case r: h = (g - b) / d + (g < b ? 6 : 0); break;
                case g: h = (b - r) / d + 2; break;
                case b: h = (r - g) / d + 4; break;
            }
            h *= 60;
        }

        return { h: h || 0, s: s * 100, l: l * 100 }; // 保证h存在
    }

    #hslToHex(h, s, l) {
        // HSL转RGB
        h = h % 360 / 60;
        s /= 100;
        l /= 100;

        const c = (1 - Math.abs(2 * l - 1)) * s;
        const x = c * (1 - Math.abs(h % 2 - 1));
        const m = l - c / 2;

        let r, g, b;
        if (h < 1) [r, g, b] = [c, x, 0];
        else if (h < 2) [r, g, b] = [x, c, 0];
        else if (h < 3) [r, g, b] = [0, c, x];
        else if (h < 4) [r, g, b] = [0, x, c];
        else if (h < 5) [r, g, b] = [x, 0, c];
        else[r, g, b] = [c, 0, x];

        // RGB转HEX
        const toHex = n => Math.round((n + m) * 255)
            .toString(16)
            .padStart(2, '0');

        return toHex(r) + toHex(g) + toHex(b);
    }

    /* 对外暴露的链式方法 */
    changeHue(maxDelta, coefficient) {
        const hsl = this.#hexToHSL(this.hex);
        const delta = this.#getNormalDelta(maxDelta, coefficient);
        hsl.h = (hsl.h + delta + 360) % 360; // 保持0-360范围
        this.hex = this.#hslToHex(hsl.h, hsl.s, hsl.l);
        return this;
    }

    changeSaturation(maxDelta, coefficient) {
        const hsl = this.#hexToHSL(this.hex);
        const delta = this.#getNormalDelta(maxDelta, coefficient);
        hsl.s = Math.max(0, Math.min(100, hsl.s + delta)); // 限制0-100
        this.hex = this.#hslToHex(hsl.h, hsl.s, hsl.l);
        return this;
    }

    changeLightness(maxDelta, coefficient) {
        const hsl = this.#hexToHSL(this.hex);
        const delta = this.#getNormalDelta(maxDelta, coefficient);
        hsl.l = Math.max(0, Math.min(100, hsl.l + delta)); // 限制0-100
        this.hex = this.#hslToHex(hsl.h, hsl.s, hsl.l);
        return this;
    }

    // 获取最终结果
    get result() {
        return this.hex;
    }
}

function isWithinRange(hex1, hex2, range) {
    // 将Hex颜色字符串转换为RGB数值
    const getRGB = (hex) => {
        const r = parseInt(hex.substring(0, 2), 16);
        const g = parseInt(hex.substring(2, 4), 16);
        const b = parseInt(hex.substring(4, 6), 16);
        return { r, g, b };
    };

    const color1 = getRGB(hex1);
    const color2 = getRGB(hex2);

    // 计算各通道的差值绝对值
    const diffRed = Math.abs(color1.r - color2.r);
    const diffGreen = Math.abs(color1.g - color2.g);
    const diffBlue = Math.abs(color1.b - color2.b);

    // 判断所有通道差值是否在允许的范围内
    return diffRed <= range && diffGreen <= range && diffBlue <= range;
}

function HSL2RGB(hsl, isFromRGB2HSL = false) {
    // 处理 H 的周期性和负值（兼容所有情况）
    let H = hsl.H % 360;
    if (H < 0) H += 360;

    // 处理 S/L 输入格式：若 isFromRGB2HSL=false 且值 >1，则视为百分比转换为小数
    const processValue = (value) => {
        if (isFromRGB2HSL) return value; // 直接使用原始值
        return value > 1 ? value / 100 : Math.max(0, Math.min(1, value));
    };

    const S = processValue(hsl.S);
    const L = processValue(hsl.L);

    let R, G, B;

    if (!isFromRGB2HSL && S === 0) {
        // 标准 HSL 的灰度情况：直接映射亮度值
        const value = Math.round(L * 255);
        R = G = B = value;
    } else {
        // 核心计算逻辑
        const C = (1 - Math.abs(2 * L - 1)) * S;
        const H_prime = H / 60;
        const X = C * (1 - Math.abs((H_prime % 2) - 1));
        const m = L - C / 2;

        // 根据色相区间确定中间颜色分量
        let [r_temp, g_temp, b_temp] = [0, 0, 0];
        if (H_prime < 1) [r_temp, g_temp, b_temp] = [C, X, 0];
        else if (H_prime < 2) [r_temp, g_temp, b_temp] = [X, C, 0];
        else if (H_prime < 3) [r_temp, g_temp, b_temp] = [0, C, X];
        else if (H_prime < 4) [r_temp, g_temp, b_temp] = [0, X, C];
        else if (H_prime < 5) [r_temp, g_temp, b_temp] = [X, 0, C];
        else[r_temp, g_temp, b_temp] = [C, 0, X];

        // 修正亮度并约束结果到 [0,255]
        R = Math.round(Math.max(0, Math.min(1, r_temp + m)) * 255);
        G = Math.round(Math.max(0, Math.min(1, g_temp + m)) * 255);
        B = Math.round(Math.max(0, Math.min(1, b_temp + m)) * 255);
    }

    const val = `RGB(${R},${G},${B})`

    return { R, G, B, val };
}

//#endregion

function HEX2RGB(hex) {
    let R = parseInt(hex.slice(0, 2), 16)
    let G = parseInt(hex.slice(2, 4), 16)
    let B = parseInt(hex.slice(-2), 16)

    const val = `RGB(${R},${G},${B})`

    return { R, G, B, val }
}

function RGB2HSL_Rate(RGB) {
    let rn = RGB.R / 255, gn = RGB.G / 255, bn = RGB.B / 255

    let tMax = Math.max(rn, gn, bn), tMin = Math.min(rn, gn, bn)
    let delta = tMax - tMin

    const L = (tMax + tMin) / 2
    const S = delta == 0 ? 0 : delta / (1 - Math.abs(2 * L - 1))
    const H = delta == 0 ? 0 : (() => {
        //  防止精度问题
        if (Math.abs(tMax - rn) < 1e-10) {
            return (gn - bn) / delta % 6 * 60
        } else if (Math.abs(tMax - gn) < 1e-10) {
            return ((bn - rn) / delta + 2) * 60
        } else {
            return ((rn - gn) / delta + 4) * 60
        }
    })()

    const val = `HSL(${H},${S}%,${L}%)`

    return { H, S, L, val }
}

function RGB2HSL(RGB) {
    const HSL_Rate = RGB2HSL_Rate(RGB)

    const H = Math.round(HSL_Rate.H < 0 ? HSL_Rate.H + 360 : HSL_Rate.H)
    const S = Math.round(HSL_Rate.S * 100)
    const L = Math.round(HSL_Rate.L * 100)

    const val = `HSL(${H},${S}%,${L}%)`

    return { H, S, L, HSL_Rate, val }
}


export {
    // ColorAdjuster,
    // isWithinRange,
    HEX2RGB,
    RGB2HSL_Rate,
    RGB2HSL,
    HSL2RGB
}